昨天將 mysql 跟 redis 都建置起來了,接著我們就要進行專案與 database 的串接,在這之前,我們最好也把我們的專案也使用容器包裝起來,然後讓所有的容器間能互相連接。
接著讓我們延續昨天的docker-compose.yaml
,並把我們的專案添加進去,首先這邊我總共新增了三隻配置檔案(包含了昨天的docker-compose.yaml
)。
depends_on:
- database_redis
- database_mysql
第一個是 depends_on 的作用是,必須等 depends_on 底下的容器都啟動之後才啟動 web_server。
第二點是 利用 volumes
volumes:
- './renco_microservices:/renco_microservices'
version: '3'
services:
database_mysql:
image: mysql:8.0.30
container_name: database_mysql
environment:
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_USER: ${DATABASE_USERNAME}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
ports:
- 3306:3306
volumes:
- ./database/mysql/sqls:/docker-entrypoint-initdb.d
- ./database/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d
networks:
net:
ipv4_address: 172.78.0.20
database_redis:
image: redis:7.0.4-alpine
container_name: database_redis
restart: always
ports:
- 6379:6379
volumes:
- ./database/redis/data:/data
- ./database/redis/redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
networks:
net:
ipv4_address: 172.78.0.21
web_server:
build:
context: .
dockerfile: ./renco_microservices/Dockerfile
container_name: renco_microservices
depends_on:
- database_redis
- database_mysql
ports:
- '3000:3000'
environment:
RENCO_MICROSERVICES: ${RENCO_MICROSERVICES}
RENCO_DATACENTER_TOKEN: ${RENCO_DATACENTER_TOKEN}
RENCO_BACKEND_URL: ${RENCO_BACKEND_URL}
RENCO_DATACENTER_URL: ${RENCO_DATACENTER_URL}
volumes:
- './web_server:/web_server'
networks:
web_net:
ipv4_address: 172.78.0.22
networks:
web_net:
ipam:
driver: default
config:
- subnet: 172.78.0.0/16
FROM node:14-alpine3.10
WORKDIR /web_server
RUN npm install pm2 -g
ENTRYPOINT ["sh", "./start.sh"]
pm2
的用途。npm install
pm2 start ./web_server.json
pm2 log
[
{
"name": "web_server",
"script": "./app.js",
"exec_mode": "cluster",
"instances": 4,
"out_file": "./pm2/web_server-out.log",
"pid_file": "./pm2/pid/web_server.pid",
"args": "3000"
}
]